package com.ms.support.jpa;

import com.googlecode.genericdao.dao.jpa.GenericDAOImpl;
import com.googlecode.genericdao.search.ExampleOptions;
import com.googlecode.genericdao.search.jpa.JPASearchProcessor;
import com.ms.support.domain.IdentifiedAuditableDomainObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.io.Serializable;
import java.util.Date;


public class BaseDAO<T, ID extends Serializable> extends GenericDAOImpl<T, ID> {
    protected static final Logger LOG = LoggerFactory.getLogger(BaseDAO.class);
    @Override
    @PersistenceContext
    public void setEntityManager(EntityManager entityManager) {
        super.setEntityManager(entityManager);

    }
    @Override
    @Autowired
    public void setSearchProcessor(JPASearchProcessor searchProcessor) {
        super.setSearchProcessor(searchProcessor);
    }

    @Override
    public void persist(T... entities) {
//        procesarCreacionAuditableEntities(entities);
        super.persist(entities);
    }

    @Override
    public T merge(T entity) {
//        procesarModificacionAuditableEntities(entity);
        return _merge(entity);
    }

    public T[] merge(T... entities) {
//        procesarModificacionAuditableEntities(entities);
        return _merge(persistentClass, entities);
    }



    private void procesarCreacionAuditableEntities(T[] entities) {
        UserDetails userDetails = null;
        for (T entity : entities) {
            if (entity instanceof IdentifiedAuditableDomainObject && ((IdentifiedAuditableDomainObject) entity).isAuditable()) {
                if (userDetails == null) {

                }
                IdentifiedAuditableDomainObject auditable = (IdentifiedAuditableDomainObject) entity;

//                auditable.setFechaCreacion(new Date());

            }
        }
    }

    private void procesarModificacionAuditableEntities(T entity) {
        UserDetails userDetails = null;

        if (entity instanceof IdentifiedAuditableDomainObject && ((IdentifiedAuditableDomainObject) entity).isAuditable()) {

            IdentifiedAuditableDomainObject auditable = (IdentifiedAuditableDomainObject) entity;

        }

    }

    private void procesarModificacionAuditableEntities(T[] entities) {
        UserDetails userDetails = null;
        for (T entity : entities) {
            if (entity instanceof IdentifiedAuditableDomainObject && ((IdentifiedAuditableDomainObject) entity).isAuditable()) {

                IdentifiedAuditableDomainObject auditable = (IdentifiedAuditableDomainObject) entity;

            }
        }
    }




    protected ExampleOptions defaultOptions() {
        ExampleOptions options = new ExampleOptions();
        options.setIgnoreCase(true);

        options.setLikeMode(ExampleOptions.ANYWHERE);
        options.excludeProp("id");
        return options;
    }




}
